/**
 * SSP
 * Copyright 2015 Baidu Inc. All rights reserved.
 *
 * @class ssp.Navigator
 * @file 全局导航栏实现
 * @author exodia
 */
define(
    function (require) {
        var u = require('underscore');
        var $ = require('jquery');
        var permission = require('er/permission');

        var exports = {};

        /**
         * @constructs Navigator
         */
        exports.constructor = function Navigator(wrapperId) {
            this.wrapperId = wrapperId;
            this.navigatorItems = null;
        };

        /**
         * 初始化导航栏
         */
        exports.init = function () {
            var wrapperContainer = document.getElementById(this.wrapperId);
            var html = [];
            u.each(
                this.navigatorItems,
                function (item) {
                    var customClass = this.customClass || '';
                    if (item.customClass) {
                        customClass += ' ' + item.customClass;
                    }
                    html.push(
                        '<li id="' + item.id + '" class="' + customClass + '">'
                        + '<a href="' + item.url + '">'
                        + item.text
                        + '</a>'
                        + '</li>'
                    );
                },
                this
            );
            wrapperContainer.innerHTML = html.join('');
            require('er/events').on('enteraction', onEnterAction, this);
        };

        /**
         * 获取导航项
         *
         * @returns {null|Array}
         */
        exports.getNavigatorItems = function () {
            return this.navigatorItems;
        };

        /**
         * 设置导航项
         *
         * @param {Array} items
         */
        exports.setNavigatorItems = function (items) {
            var authFilter = function (item) {
                // 暂时屏蔽权限
                // return true;
                var auth = item.auth;
                if (!auth) {
                    return true;
                }
                if (u.isString(auth)) {
                    auth = [auth];
                }
                return u.some(
                    auth,
                    function (authItem) {
                        return permission.isAllow(authItem);
                    }
                );
            };
            this.navigatorItems = u.filter(items, authFilter, this);
        };

        function onEnterAction(e) {
            // 子Action不影响导航
            if (e.isChildAction || e.action.group === 'error') {
                return;
            }
            var action = e.action;
            var group = action.getGroup && action.getGroup();

            var $wrapperContainer = $('#' + this.wrapperId);
            $wrapperContainer.find('li').removeClass('ui-tab-item-active');
            $wrapperContainer.find('#' + group).addClass('ui-tab-item-active');
        }

        var oo = require('eoo');

        /**
         * 获取包装容器，用于切换展开收缩状态class
         * @return {HTMLElement}
         */

        var EventTarget = require('mini-event/EventTarget');
        var Navigator = oo.create(EventTarget, exports);

        return Navigator;
    }
);
